package com.depot.auth.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import com.base.KeyGenerator;
import com.depot.domains.ResIcon;
import com.soar.BaseActionSupport;
import com.soar.context.Environment;
import com.soar.jdbc.dbhelper.DbParameter;
import com.soar.jdbc.dbhelper.DbTemplates;
import com.soar.jdbc.dbhelper.SameTypes;
import com.soar.jdbc.dbhelper.SortTypes;
import com.soar.jdbc.dbhelper.SqlTypes;
import com.soar.jdbc.dbhelper.WhereTypes;
import com.soar.util.StringUtils;

/**
 * 
 * @author Gravey Quen
 * @version $Revision$ - $Author$ at $Date$
 * @description com.itaskstar.auth.dao.ResIconDao
 */
public class ResIconDao
{
	/**
	 * 查询
	 * 
	 * @param env
	 * @param domain
	 * @return
	 * @throws SQLException
	 */
	public static List<HashMap<String, String>> select(Environment env, ResIcon domain) throws SQLException
	{
		DbParameter param = new DbParameter("RES_ICON T1");
		StringBuilder sb = new StringBuilder();
		sb.append("T1.ID,");
		sb.append("T1.ICON_GROUP,");
		sb.append("T1.ICON_CLAZZ");
		param.setSelectFields(sb.toString());
		if (StringUtils.isNoEmpty(domain.getSearchId()))
		{
			param.setWhereParams("T1.ID", domain.getSearchId(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchIconGroup()))
		{
			param.setWhereParams("T1.ICON_GROUP", domain.getSearchIconGroup(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchIconClazz()))
		{
			param.setWhereParams("T1.ICON_CLAZZ", domain.getSearchIconClazz(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		param.setOrderParams("T1.ICON_GROUP", SortTypes.ASC);
		return DbTemplates.select(env.getConnection(), param);
	}

	public static List<HashMap<String, String>> select(Environment env, ResIcon domain, BaseActionSupport action) throws SQLException
	{
		DbParameter param = new DbParameter("RES_ICON T1");
		StringBuilder sb = new StringBuilder();
		sb.append("T1.ID,");
		sb.append("T1.ICON_GROUP,");
		sb.append("T1.ICON_CLAZZ");
		param.setSelectFields(sb.toString());
		if (StringUtils.isNoEmpty(domain.getSearchId()))
		{
			param.setWhereParams("T1.ID", domain.getSearchId(), SqlTypes.CHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchIconClazz()))
		{
			param.setWhereParams("T1.ICON_CLAZZ", domain.getSearchIconClazz(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isNoEmpty(domain.getSearchIconGroup()))
		{
			param.setWhereParams("T1.ICON_GROUP", domain.getSearchIconGroup(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if (StringUtils.isEmpty(action.getSys_p_sort_field()))
		{
			action.setSys_p_sort_field("T1.ICON_GROUP");
			action.setSys_p_sort_method(SortTypes.ASC);
		}
		param.setOrderParams(action.getSys_p_sort_field(), action.getSys_p_sort_method());
		return DbTemplates.select(env.getConnection(), param, action);
	}

	/**
	 * 获取分组
	 * 
	 * @param env
	 * @param domain
	 * @return
	 * @throws SQLException
	 */
	public static List<HashMap<String, String>> selectGroup(Environment env, ResIcon domain) throws SQLException
	{
		DbParameter param = new DbParameter("RES_ICON T1");
		StringBuilder sb = new StringBuilder();
		sb.append("T1.ICON_GROUP");
		param.setSelectFields(sb.toString());
		if (StringUtils.isNoEmpty(domain.getSearchIconClazz()))
		{
			param.setWhereParams("T1.ICON_CLAZZ", domain.getSearchIconClazz(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		if(StringUtils.isNoEmpty(domain.getSearchIconGroup()))
		{
			param.setWhereParams("T1.ICON_GROUP", domain.getSearchIconGroup(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.LIKE);
		}
		param.setGroup("T1.ICON_GROUP");
		param.setOrderParams("T1.ICON_GROUP", SortTypes.DESC);
		return DbTemplates.select(env.getConnection(), param);
	}

	public static String doInsert(Environment env, ResIcon domain) throws SQLException
	{
		DbParameter param = new DbParameter("RES_ICON");
		param.setParams("ID", KeyGenerator.getInstance().getKeyId(env.getConnection()), SqlTypes.VARCHAR);
		param.setParams("ICON_GROUP", domain.getIconGroup(), SqlTypes.VARCHAR);
		param.setParams("ICON_CLAZZ", domain.getIconClazz(), SqlTypes.VARCHAR);
		if (!DbTemplates.insert(env.getConnection(), param))
		{
			return "0";
		}
		return "1";
	}

	public static String doSave(Environment env, ResIcon domain) throws SQLException
	{
		if (domain.getIds() == null)
		{
			return "0";
		}
		DbParameter param = null;
		for (int i = 0; i < domain.getIds().length; i++)
		{
			String id = domain.getIds()[i].split("_")[0];
			int index = Integer.parseInt(domain.getIds()[i].split("_")[1]);
			param = new DbParameter("RES_ICON");
			param.setParams("ICON_GROUP", domain.getIconGroups()[index], SqlTypes.VARCHAR);
			param.setParams("ICON_CLAZZ", domain.getIconClazzs()[index], SqlTypes.VARCHAR);
			param.setWhereParams("ID", id, SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.YES);
			if (!DbTemplates.update(env.getConnection(), param))
			{
				env.getConnection().rollback();
				return "0";
			}
		}
		return "1";
	}

	public static String dodelete(Environment env, String[] ids) throws SQLException
	{
		DbParameter param = new DbParameter("RES_ICON");
		param.setWhereParams("ID", param.getInSQL(ids), SqlTypes.CUSTOM, WhereTypes.AND, SameTypes.IN);
		if (!DbTemplates.delete(env.getConnection(), param))
		{
			return "0";
		}
		return "1";
	}

	public static String indata(Environment env, String[] data, String group) throws SQLException
	{
		DbParameter param = new DbParameter("RES_ICON");
		for (String str : data)
		{
			param = new DbParameter("RES_ICON");
			param.setParams("ID", KeyGenerator.getInstance().getKeyId(env.getConnection()));
			param.setParams("ICON_CLAZZ", str.trim());
			param.setParams("ICON_GROUP", group);
			DbTemplates.insert(env.getConnection(), param);
		}
		return "1";
	}
}
